package cobots.swarm.agents;

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Random;
import java.util.Vector;

import cobots.swarm.main.SwarmState;


public class PathFollowingAgent extends LeaderAgent {

	protected Rectangle2D rect;
	protected Point2D destination = new Point2D.Double();
	protected Random r = new Random();
	int pathIndex = 0;
	public PathFollowingAgent(int id) {
		super(id);
		rect = new Rectangle2D.Double(-SwarmState.width/2, -SwarmState.height/2, SwarmState.width, SwarmState.height);
		destination = SwarmState.getPath(this.id).firstElement();
	}

		@Override
	public Point2D computeMovement() {
		if (targetId != -1) {
			Target t = SwarmState.target(targetId);
			return moveTowards(t);
		}
		else {
			if (destination == null)
				destination = SwarmState.getPath(id).firstElement();
			else {
				Vector<Point2D> path = SwarmState.getPath(id);
				if (distance(destination) < 1 || path.size() <= pathIndex || !destination.equals(path.elementAt(pathIndex))) {			
					pathIndex++;
					if (pathIndex >= path.size())
						pathIndex = 0;
					destination = path.elementAt(pathIndex);
				}
			}
			
			if (distance(destination) > 0)
				return moveTowards(destination);
			else
				return new Point2D.Double(0,0);
		}		
	}


}
